import pymysql

conn = None

cursor = None

try:
    conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='test01', charset='utf8')

    cursor = conn.cursor()

    cursor.execute("insert into t_book(id,title,pub_date) values (15,'青之箱','2025-03-31');")

    print("影响的行数:",conn.affected_rows())

    conn.commit()



except Exception as e:
    print("插入数据错误:", str(e))
    #回滚事务
    conn.rollback()

finally:

    cursor.close()

    conn.close()